package online.sh.cn.fy.users.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.DES3Util;
import online.sh.cn.fy.common.utils.ResponseUtils;
import online.sh.cn.fy.common.utils.TokenUtils;

/**
 * 注销用户
 * 迁移存档
 */
@WebServlet("/canceluser")
public class CancelUserServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String openidd=CookieUtil.getCookieValueByName(request, "openid");
		try {
			openidd=DES3Util.decrypt(openidd);
		} catch (Exception e) {
			log(e.getMessage());
		}
		JSONObject json=new JSONObject();
		if(null!=openidd && ""!=openidd){
			TokenUtils tou=new TokenUtils();
			Map cancel=tou.selectFyUserinfoMapAll(openidd);
			String openid=cancel.get("openid").toString();
			int experience=(int)cancel.get("experience");
			int points=(int)cancel.get("points");
			String level=cancel.get("level").toString();
			String nickname=cancel.get("nickname").toString();
			String mobile=cancel.get("mobile").toString();
			String belong=cancel.get("belong").toString();
			String operator=cancel.get("operator").toString();
			String fname=cancel.get("fname").toString();
			String school=cancel.get("school").toString();
			String schoolbuilding=cancel.get("schoolbuilding").toString();
			String schoolgrade=cancel.get("schoolgrade").toString();
			String schoolmajor=cancel.get("schoolmajor").toString();
			String headimg=cancel.get("headimg").toString();
			String bindingtime=cancel.get("bindingtime").toString();
			String subscribetime=cancel.get("subscribetime").toString();
			//插入注销用户信息归档
			tou.insertCancelUser(openid, experience, points, level, nickname, mobile, belong, operator, fname, school, schoolbuilding, schoolgrade, schoolmajor, headimg, bindingtime, subscribetime);
			//删除用户信息
			tou.deleteCancelUser(openid);
			tou.deleteEventsUser(openid);
			tou.deleteSignUser(openid);
			System.out.println(openid+":已成功注销，数据归档:"+nickname);
			json.put("result", "0");   //注销成功
			ResponseUtils.renderJson(response, json.toString());
		}else{
			json.put("result", "1");   //openid未获取
			ResponseUtils.renderJson(response, json.toString());
		}
	}

}
